package com.nangle.managehost.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.nangle.managehost.R;
import com.nangle.managehost.entity.Patient;
import com.nangle.managehost.entity.Room;
import com.nangle.managehost.utils.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：Administrator on 2016/9/21 16:59
 * 描述：
 */
public class BedGridViewAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context mContext;
    private List<Room> mDatas;



    public BedGridViewAdapter(Context context, List<Room> mDatas) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.bedcard_itemlayout, parent,false);

            viewHolder = new ViewHolder();
            viewHolder.doctorLeader = (TextView)convertView.findViewById(R.id.room_doctor_leader);
            viewHolder.nurseLeader = (TextView)convertView.findViewById(R.id.room_nurse_leader);
            viewHolder.doctor = (TextView)convertView.findViewById(R.id.room_doctor);
            viewHolder.nurse = (TextView)convertView.findViewById(R.id.room_nurse);
            viewHolder.room_number = (TextView)convertView.findViewById(R.id.tv_room_number);
            viewHolder.room_type = (TextView)convertView.findViewById(R.id.tv_room_vip);
            viewHolder.llyRoomType = (LinearLayout)convertView.findViewById(R.id.lly_room_type);
            viewHolder.sickbed_bednumber1 = (TextView)convertView.findViewById(R.id.sickbed_bednumber1);
            viewHolder.sickbed_name1 = (TextView)convertView.findViewById(R.id.sickbed_name1);
            viewHolder.sickbed_bednumber2 = (TextView)convertView.findViewById(R.id.sickbed_bednumber2);
            viewHolder.sickbed_name2 = (TextView)convertView.findViewById(R.id.sickbed_name2);
            viewHolder.sickbed_bednumber3 = (TextView)convertView.findViewById(R.id.sickbed_bednumber3);
            viewHolder.sickbed_name3 = (TextView)convertView.findViewById(R.id.sickbed_name3);
            viewHolder.sickbed_bednumber4 = (TextView)convertView.findViewById(R.id.sickbed_bednumber4);
            viewHolder.sickbed_name4 = (TextView)convertView.findViewById(R.id.sickbed_name4);
            viewHolder.sickbed_bednumber5 = (TextView)convertView.findViewById(R.id.sickbed_bednumber5);
            viewHolder.sickbed_name5 = (TextView)convertView.findViewById(R.id.sickbed_name5);
            viewHolder.sickbed_bednumber6 = (TextView)convertView.findViewById(R.id.sickbed_bednumber6);
            viewHolder.sickbed_name6 = (TextView)convertView.findViewById(R.id.sickbed_name6);
            viewHolder.sickbed_bednumber7 = (TextView)convertView.findViewById(R.id.sickbed_bednumber7);
            viewHolder.sickbed_name7 = (TextView)convertView.findViewById(R.id.sickbed_name7);
            viewHolder.sickbed_bednumber8 = (TextView)convertView.findViewById(R.id.sickbed_bednumber8);
            viewHolder.sickbed_name8 = (TextView)convertView.findViewById(R.id.sickbed_name8);
            viewHolder.sickbed_bednumber9 = (TextView)convertView.findViewById(R.id.sickbed_bednumber9);
            viewHolder.sickbed_name9 = (TextView)convertView.findViewById(R.id.sickbed_name9);
            viewHolder.F1 = (FrameLayout)convertView.findViewById(R.id.f1);
            viewHolder.F4 = (FrameLayout)convertView.findViewById(R.id.f4);
            viewHolder.F2 = (FrameLayout)convertView.findViewById(R.id.f2);
            viewHolder.F3 = (FrameLayout)convertView.findViewById(R.id.f3);
            viewHolder.F5 = (FrameLayout)convertView.findViewById(R.id.f5);
            viewHolder.F6 = (FrameLayout)convertView.findViewById(R.id.f6);
            viewHolder.F7 = (FrameLayout)convertView.findViewById(R.id.f7);
            viewHolder.F8 = (FrameLayout)convertView.findViewById(R.id.f8);
            viewHolder.F9 = (FrameLayout)convertView.findViewById(R.id.f9);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Room room = mDatas.get(position);
        viewHolder.doctorLeader.setText(room.getDoctorLeader());
        viewHolder.nurseLeader.setText(room.getNurseLeader());
        viewHolder.doctor.setText(room.getDoctor());
        viewHolder.nurse.setText(room.getNurse());
        Map<Integer,TextView> beds = new HashMap<>();
        if(beds.size()<=0){
            beds.put(0,viewHolder.sickbed_bednumber1);
            beds.put(1,viewHolder.sickbed_bednumber2);
            beds.put(2,viewHolder.sickbed_bednumber3);
            beds.put(3,viewHolder.sickbed_bednumber4);
            beds.put(4,viewHolder.sickbed_bednumber5);
            beds.put(4,viewHolder.sickbed_bednumber6);
            beds.put(5,viewHolder.sickbed_bednumber7);
            beds.put(6,viewHolder.sickbed_bednumber8);
            beds.put(7,viewHolder.sickbed_bednumber9);
        }
        Map<Integer,TextView> names = new HashMap<>();
        Map<Integer,FrameLayout> fs = new HashMap<>();
        if(names.size()<=0){
            names.put(0,viewHolder.sickbed_name1);
            names.put(1,viewHolder.sickbed_name2);
            names.put(2,viewHolder.sickbed_name3);
            names.put(3,viewHolder.sickbed_name4);
            names.put(4,viewHolder.sickbed_name5);
            names.put(4,viewHolder.sickbed_name6);
            names.put(5,viewHolder.sickbed_name7);
            names.put(6,viewHolder.sickbed_name8);
            names.put(7,viewHolder.sickbed_name9);
        }
        if(fs.size()<=0){
            fs.put(0,viewHolder.F1);
            fs.put(1,viewHolder.F2);
            fs.put(2,viewHolder.F3);
            fs.put(3,viewHolder.F4);
            fs.put(4,viewHolder.F5);
            fs.put(4,viewHolder.F6);
            fs.put(5,viewHolder.F7);
            fs.put(6,viewHolder.F8);
            fs.put(7,viewHolder.F9);
        }
        viewHolder.room_number.setText(room.getRoomnumber()+"");
        if(room.getType()==0){
            viewHolder.llyRoomType.setBackgroundResource(R.drawable.bednumber_icon);
            viewHolder.room_type.setText("房");
        }else{
            viewHolder.llyRoomType.setBackgroundResource(R.drawable.vipnumber_icon);
            viewHolder.room_type.setText("VIP");
        }


        List<Patient> patients = room.getPatients();
        int count = 0;
        if(patients!=null){
            count = patients.size();
        }
        for(int i= 0;i<8;i++){
            if(i<count){
                beds.get(i).setVisibility(View.VISIBLE);
                beds.get(i).setText(patients.get(i).getBed()+"床");
                names.get(i).setVisibility(View.VISIBLE);
                names.get(i).setText(patients.get(i).getName());
                fs.get(i).setVisibility(View.VISIBLE);
            }else{
                fs.get(i).setVisibility(View.INVISIBLE);
                beds.get(i).setVisibility(View.INVISIBLE);
                names.get(i).setVisibility(View.INVISIBLE);
            }
        }

        return convertView;
    }

    private final class ViewHolder {
        private TextView doctorLeader;
        private TextView nurseLeader;
        private TextView doctor;
        private TextView nurse;
        private TextView room_number;
        private TextView room_type;

        private TextView sickbed_bednumber1;
        private TextView sickbed_name1;

        private TextView sickbed_bednumber2;
        private TextView sickbed_name2;

        private TextView sickbed_bednumber3;
        private TextView sickbed_name3;

        private TextView sickbed_bednumber4;
        private TextView sickbed_name4;

        private TextView sickbed_bednumber5;
        private TextView sickbed_name5;

        private TextView sickbed_bednumber6;
        private TextView sickbed_name6;

        private TextView sickbed_bednumber7;
        private TextView sickbed_name7;

        private TextView sickbed_bednumber8;
        private TextView sickbed_name8;

        private TextView sickbed_bednumber9;
        private TextView sickbed_name9;

        private LinearLayout llyRoomType;
        private FrameLayout F1;
        private FrameLayout F2;
        private FrameLayout F3;
        private FrameLayout F4;
        private FrameLayout F5;
        private FrameLayout F6;
        private FrameLayout F7;
        private FrameLayout F8;
        private FrameLayout F9;



    }


}